// JiraMapping.js
class TicketStatusMapping extends JiraMapping
{
    mapping()
    {
        return {
            key: 'key',
            fields: {
                fixVersions: 'fixVersion',
                status: 'status',
                customfield_13431: 'ticketAction',
                customfield_15300: 'areaImpact',
                customfield_14800: 'region',
                customfield_13423: 'countryCovered',
                customfield_15820: 'codeMerge',
                customfield_15819: 'branch'
            }
        };
    }

    afterConvert(fields)
    {
        console.log('before:', fields);
        let type = jQuery('#' + fields.key + ' .issuetype img').attr('alt');

        if (!fields.ticketAction) {
            fields.ticketAction = '<span style="color:red">没有维护 Ticket Action</span>'
            fields.codeMerge = '';
            fields.branch = '';
        } else {
            if (fields.ticketAction) {
                fields.ticketAction = fields.ticketAction.value;
            } else {
                fields.ticketAction = '<span style="color:red;">Ticket Action 没有维护</span>';
            }

            // branch
            if (fields.ticketAction != 'Code change' && fields.branch) {
                fields.branch = '<span style="color:red">TicketAction 不是 Code change 但检查到有分支</span>';
            }

            //
            if (fields.ticketAction == 'Code change') {
                if (!fields.codeMerge) {
                    fields.codeMerge = '<span style="color:red">TicketAction 是 Code change 但没有合并分支</span>';
                } else {
                    if (fields.codeMerge) {
                        fields.codeMerge = fields.codeMerge.value;
                    }
                }

                if (!fields.branch) {
                    fields.branch = '<span style="color:red">TicketAction 是 Code change 但没有检查到分支</span>';
                }

                if (fields.codeMerge && fields.codeMerge == 'No') {
                    fields.codeMerge = '<span style="color:red">No</span>';
                } else {
                    if (!fields.codeMerge) {
                        fields.codeMerge = '<span style="color:red">No</span>';
                    } else {
                        fields.codeMerge = fields.codeMerge.value;
                    }
                }
            }

            fields.ticketAction = '<span style="color:#FF00FF;">' + fields.ticketAction + '<span>';
        }

        //
        if (type =='Bug') {
            if (!fields.codeMerge) {
                fields.codeMerge = '<span style="color:red">Ticket 是 Bug 但没有合并分支</span>';
            } else {
                if (fields.codeMerge) {
                    fields.codeMerge = fields.codeMerge.value;
                }
            }

            if (!fields.branch) {
                fields.branch = '<span style="color:red">Ticket 是 Bug 但没有检查到分支</span>';
            }
        }

        if (type=='Task') {
            if (fields.codeMerge) {
                fields.codeMerge = fields.codeMerge.value;
            }

            if (fields.branch) {
                fields.branch = fields.branch;
            }
        }

        //
        if (!fields.areaImpact) {
            fields.areaImpact = '<span style="color:red;>Area Impace 未维护</span>';
        }

        if (fields.region) {
            let region = [];
            fields.region.forEach(item => {
                region.push(item.value);
            });
            fields.region = '<span>' + region.join(',') + '<span>';
        }

        if (fields.countryCovered) {
            let countryCovered = [];
            fields.countryCovered.forEach(item => {
                countryCovered.push(item.value);
            });
            fields.countryCovered = countryCovered.join(',');
        }

        fields.status = '<span style="color:blue;">' + fields.status.name + '</span>';

        if (fields.fixVersion) {
            let fixVersion = [];
            fields.fixVersion.forEach(item => {
                fixVersion.push(item.name);
            });
            fields.fixVersion = fixVersion.join(',');
        }

        if (fields.areaImpact) {
            fields.areaImpact = fields.areaImpact.value;
        }

        console.log('after:', fields);
        return fields;
    }
}